package com.perdragon.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.perdragon.entity.User;
import com.perdragon.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;

	@RequestMapping("/list")
	public ModelAndView listUser(String page) {
		int pageNum = 1;
		if (page != null)
			pageNum = Integer.parseInt(page);
		int rowNum = userService.getRowNum();
		int maxPageNum = (int) Math.ceil(rowNum / 10.0);
		if (maxPageNum == 0)
			maxPageNum = 1;
		if (pageNum >= 1 && pageNum <= maxPageNum) {
			ModelAndView mv = new ModelAndView();
			mv.setViewName("user/listUser");
			mv.addObject("users", userService.listUser((pageNum - 1) * 10));
			mv.addObject("pageNum", pageNum);
			mv.addObject("maxPageNum", maxPageNum);
			return mv;
		} else {
			ModelAndView mv = new ModelAndView("redirect:/user/list");
			return mv;
		}
	}

	@RequestMapping("/delete")
	public ModelAndView deleteUser(String id) {
		userService.cancellation(Integer.parseInt(id));
		ModelAndView mv = new ModelAndView("redirect:/user/list");
		return mv;
	}

	@GetMapping("/update")
	public ModelAndView updateUser(String id) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("user/updateUser");
		mv.addObject("u", userService.getOneUser(Integer.parseInt(id)));
		return mv;
	}

	@PostMapping("/update")
	public ModelAndView updateUser(String id, String username, String password, String sex, String birthday,
			@RequestParam("authority") Byte isadmin, String content) throws ParseException {
		User user = new User();
		user.setId(Integer.valueOf(id));
		user.setUsername(username);
		user.setPassword(password);
		user.setSex(Byte.parseByte(sex));
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		user.setBirthday(df.parse(birthday));
		user.setIsadmin(isadmin);
		user.setContent(content);
		userService.updateUser(user);
		ModelAndView mv = new ModelAndView("redirect:/user/list");
		return mv;
	}

	@GetMapping("/add")
	public ModelAndView addUser() {
		ModelAndView mv = new ModelAndView("user/addUser");
		return mv;
	}

	@PostMapping("/add")
	public ModelAndView doAddUser(String username, String password, String sex, String birthday,
			@RequestParam("authority") String isadmin, String content) throws ParseException {
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setSex(Byte.parseByte(sex));
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		user.setBirthday(df.parse(birthday));
		Date createtime = new Date(System.currentTimeMillis());
		user.setCreatetime(createtime);
		user.setIsadmin(Byte.parseByte(isadmin));
		user.setContent(content);
		userService.addUser(user);
		ModelAndView mv = new ModelAndView("redirect:/user/list");
		return mv;
	}

	@GetMapping("/login")
	public ModelAndView login() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("user/loginUser");
		return mv;
	}

	@PostMapping("/login")
	public ModelAndView login(HttpSession session, String username, String password) {
		User user = userService.loginUser(username, password);
		ModelAndView mv = new ModelAndView();
		if (user != null) {
			mv.setViewName("redirect:/");
			session.setAttribute("user", user);
		} else {
			mv.setViewName("user/loginUser");
			mv.addObject("messageInfo", "用户名或密码错误！");
		}
		return mv;
	}

	@RequestMapping("/logout")
	public ModelAndView tologout(HttpSession session, SessionStatus sessionStatus) {
		session.removeAttribute("user");
		ModelAndView mv = new ModelAndView("redirect:/user/login");
		return mv;
	}
}
